package com.cts.tmall.biz.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cts.tmall.app.response.PropSpecResponse;
import com.cts.tmall.dal.entity.Prop;
import com.cts.tmall.dal.entity.Spec;
import com.cts.tmall.dal.mapper.PropMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class PropService extends ServiceImpl<PropMapper, Prop> {

    @Autowired
    SpecService specService;

    public List<PropSpecResponse> getPropSpec(String goodsNo) {
        List<PropSpecResponse> propSpecResponseList = new ArrayList<>();
        List<Prop> propList = this.list(new QueryWrapper<Prop>().eq("goods_no", goodsNo));
        if (propList.size() == 0) {
            return propSpecResponseList;
        }
        for (Prop prop : propList) {
            PropSpecResponse propSpecResponse = new PropSpecResponse();
            BeanUtils.copyProperties(prop, propSpecResponse);
            List<Spec> specList = specService.list(new QueryWrapper<Spec>().eq("prop_no", prop.getPropNo()));
            propSpecResponse.setSpecList(specList);
            propSpecResponseList.add(propSpecResponse);
        }
        return propSpecResponseList;
    }
}
